package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.business.interfaces.tmall.OrderTmallExtendBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.model.dto.order.proxy.SignInDTO;
import com.zmn.oms.model.dto.tmall.OrderTmallExtendDTO;
import com.zmn.oms.model.dto.work.modify.OrderSignInDTO;
import com.zmn.oms.model.entity.tamllextend.OrderTmallExtend;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.third.taobao.service.TmallService;
import com.zmn.oms.zmn.business.interfaces.tmall.TmallOrderBService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
/**
 * 类描述：签到
 *
 * @author yule
 * @date 2019/10/16 20:45
 */
@Controller
@RequestMapping("/work/modify")
public class WorkSignInController extends WorkBaseController {

    @Autowired
    private OrderTmallExtendBService orderTmallExtendBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    private TmallService tmallService;
    @Autowired
    private TmallOrderBService tmallOrderBService;

    /**
     * 签到
     */
    @RequestMapping(value = "/sign/in")
    public ModelAndView signIn(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/signin");
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(orderId, workId);

        OrderTmallExtendDTO orderTmallExtendDTO = new OrderTmallExtendDTO();
        orderTmallExtendDTO.setOrderId(orderId);
        orderTmallExtendDTO.setSignInStatus(GlobalConsts.YES);
        try {
            OrderTmallExtend orderTmallExtend = orderTmallExtendBService.findSignInByTmallQuery(orderTmallExtendDTO);
            mv.addObject("signInTimeStampAdd15Min", orderTmallExtend.getSignInTime() == null ? 0 : DateUtil.addMinute(orderTmallExtend.getSignInTime(), 15).getTime());//十五分钟倒计时时间戳
        } catch (Exception e) {
            e.printStackTrace();
        }
        mv.addObject("orderWork", orderWork);

        return mv;
    }

    /**
     * 保存及推送签到消息
     */
    @RequestMapping(value = "/save/sign/in")
    @ResponseBody
    public ResultDTO updateSignIn(@RequestBody @Valid OrderSignInDTO orderSignInDTO) {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(orderSignInDTO.getOrderId(), orderSignInDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_SIGN_IN);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        try {
            //推送天猫签到消息
            logger.info("保存签到-推送天猫签到消息开始:" + orderSignInDTO);
            boolean isSignIn = tmallService.syncSignIn(orderSignInDTO.getOrderId(), null);
            logger.info("保存签到-推送天猫签到消息结束:" + isSignIn);
            if (isSignIn) {
                SignInDTO signInDTO = BeanMapper.map(orderSignInDTO, SignInDTO.class);
                signInDTO.setSignInStatus(GlobalConsts.YES);
                tmallOrderBService.signIn(signInDTO);
            } else {
                return ResultDTO.fail("签到失败");
            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ResultDTO.fail("签到失败");
        }
        return ResultDTO.success();
    }
}
